package com.zjj.controller;

import java.util.Random;
import java.util.concurrent.Future;

import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.AsyncResult;
import org.springframework.stereotype.Component;

@Component
public class Task {
	
	@Async
	public void task1() throws Exception {
		System.out.println("task1 开始执行------------");
		long time1 = System.currentTimeMillis();
		Thread.sleep(new Random().nextInt(4000));
		long time2 = System.currentTimeMillis();
		System.out.println("task1任务耗时:" + (time2 - time1) + "ms");
	}

	@Async
	public void task2() throws Exception {
		System.out.println("task2开始执行------------");
		long time1 = System.currentTimeMillis();
		Thread.sleep(new Random().nextInt(4000));
		long time2 = System.currentTimeMillis();
		System.out.println("task2任务耗时:" + (time2 - time1) + "ms");
	}
	
	@Async
	public void task3() throws Exception {
		System.out.println("task3 开始执行------------");
		long time1 = System.currentTimeMillis();
		Thread.sleep(new Random().nextInt(4000));
		long time2 = System.currentTimeMillis();
		System.out.println("task3任务耗时:" + (time2 - time1) + "ms");
	}
	
	@Async
	public Future<String> futureTask1() throws Exception {
		System.out.println("futureTask1 开始执行------------");
		long time1 = System.currentTimeMillis();
		Thread.sleep(new Random().nextInt(4000));
		long time2 = System.currentTimeMillis();
		System.out.println("futureTask1任务耗时:" + (time2 - time1) + "ms");
		return new AsyncResult<>("futureTask1任务完成");
	}

	@Async
	public Future<String> futureTask2() throws Exception {
		System.out.println("futureTask2开始执行------------");
		long time1 = System.currentTimeMillis();
		Thread.sleep(new Random().nextInt(4000));
		long time2 = System.currentTimeMillis();
		System.out.println("futureTask2任务耗时:" + (time2 - time1) + "ms");
		return new AsyncResult<>("futureTask2任务完成");
	}
	
	@Async
	public Future<String> futureTask3() throws Exception {
		System.out.println("futureTask3开始执行------------");
		long time1 = System.currentTimeMillis();
		Thread.sleep(new Random().nextInt(4000));
		long time2 = System.currentTimeMillis();
		System.out.println("futureTask3任务耗时:" + (time2 - time1) + "ms");
		return new AsyncResult<>("futureTask3任务完成");
	}
}
